﻿Imports TransportadoraToledoBLL
Imports System.IO

Public Class frm_cadastro_combustivel
    Dim objBLL As AbastecimentoBLL
    Dim objV As VeiculosBLL
    Dim tab, tab2 As DataTable
    Public controle As String
    Public ultimo, primeiro, atual, vei_codigo As Integer
#Region "Métodos"
    Public Sub carregaPropriedades()
        Try
            objBLL = New AbastecimentoBLL
            If (txtCodigo.Text = "") Then
                objBLL.pAba_codigo = 0
            Else
                objBLL.pAba_codigo = txtCodigo.Text
            End If
            objBLL.pAba_data = dtDataAbastecimento.Value
            If (txtKmAtual.Text = "") Then
                objBLL.pAba_km = 0
            Else
                objBLL.pAba_km = txtKmAtual.Text
            End If
            If (txtValor.Text = "") Then
                objBLL.pAba_valor = 0
            Else
                objBLL.pAba_valor = txtValor.Text.Replace("R$", "")
            End If
            objBLL.pVei_codigo = vei_codigo
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub
    Public Sub carregaCampos()
        Try
            txtCodigo.Text = objBLL.pAba_codigo
            txtKmAtual.Text = objBLL.pAba_km
            txtValor.Text = objBLL.pAba_valor.ToString("C2")
            dtDataAbastecimento.Value = objBLL.pAba_data
            vei_codigo = objBLL.pVei_codigo
            objV = New VeiculosBLL
            objV.localizar(vei_codigo)
            txtVeiculo.Text = objV.pPlaca
            objV = Nothing
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub
    Public Sub carregaAbasVeiculo(ByVal codigo As Integer)
        Try
            objBLL = New AbastecimentoBLL
            Dim tab As DataTable
            tab = objBLL.localizarAbasVeiculo(codigo)
            If (tab.Rows.Count > 0) Then
                dgAbastecimentos.DataSource = tab
                dgAbastecimentos.Columns("aba_valor").DefaultCellStyle.Format = "R$ ###,###0.00"
            End If
            'objBLL = Nothing
        Catch ex As Exception
            Throw ex
        End Try
    End Sub
    Public Sub associaGrid()
        Try
            tab = New DataTable
            tab.Columns.Add("aba_codigo", GetType(Integer))
            tab.Columns.Add("mod_nome", GetType(String))
            tab.Columns.Add("aba_valor", GetType(Double))
            tab.Columns.Add("aba_data", GetType(DateTime))
            dgConsulta.DataSource = tab
            dgConsulta.Columns("aba_codigo").HeaderText = "Código"
            dgConsulta.Columns("aba_codigo").Width = 90
            dgConsulta.Columns("mod_nome").HeaderText = "Veículo"
            dgConsulta.Columns("mod_nome").Width = 280
            dgConsulta.Columns("aba_data").HeaderText = "Data"
            dgConsulta.Columns("aba_data").Width = 120
            dgConsulta.Columns("aba_valor").HeaderText = "Valor"
            dgConsulta.Columns("aba_valor").Width = 120
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub
    Public Sub associaGridAbastecimentos()
        Try
            tab2 = New DataTable
            tab2.Columns.Add("aba_data", GetType(DateTime))
            tab2.Columns.Add("aba_km", GetType(Integer))
            tab2.Columns.Add("aba_valor", GetType(Double))
            dgAbastecimentos.DataSource = tab2
            dgAbastecimentos.Columns("aba_data").HeaderText = "Data"
            dgAbastecimentos.Columns("aba_km").HeaderText = "Kilometragem"
            dgAbastecimentos.Columns("aba_valor").HeaderText = "Valor"
            dgAbastecimentos.Columns("aba_valor").Width = 70
            dgAbastecimentos.Columns("aba_km").Width = 100
            dgAbastecimentos.Columns("aba_valor").Width = 100
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub
    Public Sub limpar()
        txtCodigo.Text = ""
        txtKmAtual.Text = ""
        txtValor.Text = ""
        txtVeiculo.Text = ""
        dtDataAbastecimento.Value = Date.Now
    End Sub
    Sub carregaPrimeiroUltimo()
        Try
            objBLL = New AbastecimentoBLL
            ultimo = objBLL.localizarPrimeiroUltimo("ultimo")
            primeiro = objBLL.localizarPrimeiroUltimo("primeiro")
            atual = ultimo
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
        objBLL = Nothing
    End Sub
    Sub carregaUltimo()
        If (ultimo <> 0) Then
            'algum usuario foi clicado
            objBLL = New AbastecimentoBLL
            objBLL.localizar(ultimo)
            If (objBLL.pAba_codigo <> 0) Then
                carregaCampos()
            End If
        End If
        objBLL = Nothing
    End Sub
    Sub carregaPrimeiro()
        If (primeiro <> 0) Then
            'algum usuario foi clicado
            objBLL = New AbastecimentoBLL
            objBLL.localizar(primeiro)
            If (objBLL.pAba_codigo <> 0) Then
                carregaCampos()
            End If
        End If
        objBLL = Nothing
    End Sub
    Sub status(ByVal sta As Boolean)

        If (txtCodigo.Text = "" Or controle = "A") Then
            bIncluir.Enabled = sta
            If (txtCodigo.Text = "") Then
                bAlterar.Enabled = False
                bVaPara.Enabled = False
                bPesquisar.Enabled = False
                bExcluir.Enabled = False
                gbConsulta.Enabled = False
            Else
                bExcluir.Enabled = sta
                bPesquisar.Enabled = sta
                bAlterar.Enabled = sta
                bVaPara.Enabled = sta
            End If
            bCancelar.Enabled = Not sta
            bSalvar.Enabled = Not sta
            bPrimeiro.Enabled = sta
            bProximo.Enabled = sta
            bUltimo.Enabled = sta
            bAnterior.Enabled = sta
            gbConteudo.Enabled = Not sta

        Else
            bIncluir.Enabled = sta

            If (txtCodigo.Text = "") Then
                bExcluir.Enabled = False
                bAlterar.Enabled = False
                bVaPara.Enabled = False
                bPesquisar.Enabled = False
            Else
                bExcluir.Enabled = sta
                bAlterar.Enabled = sta
                bVaPara.Enabled = sta
                bPesquisar.Enabled = sta
            End If

            bCancelar.Enabled = Not sta
            bSalvar.Enabled = Not sta

            gbConteudo.Enabled = Not sta
        End If

        If (atual = primeiro And atual = ultimo) Then
            bProximo.Enabled = False
            bUltimo.Enabled = False
            bAnterior.Enabled = False
            bPrimeiro.Enabled = False
        ElseIf (atual = ultimo) Then
            bProximo.Enabled = False
            bUltimo.Enabled = False
            bAnterior.Enabled = True
            bPrimeiro.Enabled = True
        ElseIf (atual = primeiro) Then
            bProximo.Enabled = True
            bUltimo.Enabled = True
            bAnterior.Enabled = False
            bPrimeiro.Enabled = False
        Else
            bProximo.Enabled = True
            bUltimo.Enabled = True
            bAnterior.Enabled = True
            bPrimeiro.Enabled = True
        End If
    End Sub
    Public Sub inserir()
        Dim ret As Boolean
        Try
            'objBLL = New AbastecimentoBLL
            carregaPropriedades()
            If (txtCodigo.Text = "") Then
                ret = objBLL.inserir
            Else
                ret = objBLL.alterar
            End If
            If (ret) Then
                MessageBox.Show("Registro Salvo com Sucesso!", "Mensagem do Sistema", MessageBoxButtons.OK, MessageBoxIcon.Information)
                limpar()
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Erro", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
        End Try
    End Sub
#End Region
    Private Sub bSair_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Me.Close()
    End Sub

    Private Sub frm_cadastro_combustivel_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        carregaPrimeiroUltimo()
        carregaUltimo()
        bCancelar_Click(sender, e)
        associaGrid()
        associaGridAbastecimentos()
        gbConsulta.Visible = False
        Me.Location = New System.Drawing.Point(Me.Location.X, Me.Location.Y + 127)
        Me.Size = New Size(716, 295)
        carregaAbasVeiculo(vei_codigo)
    End Sub

    Private Sub bCancelar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bCancelar.Click
        If (controle = "I") Then
            carregaPrimeiroUltimo()
            carregaUltimo()
        End If
        controle = ""
        status(True)
    End Sub

    Private Sub bAlterar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bAlterar.Click
        controle = "A"
        status(False)
    End Sub

    Private Sub bVaPara_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bVaPara.Click
        Try
            Dim frm As New frm_VaPara("combustivel")
            frm.ShowDialog()
            If (frm.codigo <> 0) Then
                'algum usuario foi clicado
                objBLL = New AbastecimentoBLL
                objBLL.localizar(frm.codigo)
                If (objBLL.pAba_codigo <> 0) Then
                    carregaCampos()
                    atual = objBLL.pAba_codigo
                End If
            End If
            status(True)
            frm = Nothing
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        Finally
            objBLL = Nothing
        End Try
    End Sub

    Private Sub bSalvar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bSalvar.Click
        Try
            carregaPropriedades()
            If (controle = "I") Then
                objBLL.inserir()
                MessageBox.Show("Incluido com sucesso!", "TransToledo", MessageBoxButtons.OK, MessageBoxIcon.Asterisk)
                limpar()
                carregaPrimeiroUltimo()
                carregaUltimo()
                status(True)
            Else
                objBLL.alterar()
                MessageBox.Show("Alterado com sucesso!", "TransToledo", MessageBoxButtons.OK, MessageBoxIcon.Asterisk)
                status(True)
            End If

        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub

    Private Sub bExcluir_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bExcluir.Click
        Try
            If (MessageBox.Show("Deseja excluir?", "", MessageBoxButtons.YesNo) = DialogResult.Yes) Then
                carregaPropriedades()
                objBLL.excluir()
                MessageBox.Show("Excluido com sucesso!")
                limpar()
                carregaPrimeiroUltimo()
                carregaUltimo()
                status(True)
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub

    Private Sub bPesquisar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bPesquisar.Click
        gbConsulta.Enabled = True
        gbConsulta.Visible = True
        bPesquisar.Enabled = False
        Me.Location = New System.Drawing.Point(Me.Location.X, Me.Location.Y - 127)
        Me.Size = New Size(716, 563)
    End Sub

    Private Sub bIncluir_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bIncluir.Click
        limpar()
        bVeiculos.Focus()
        controle = "I"
        status(False)
    End Sub

    Private Sub bPrimeiro_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bPrimeiro.Click
        carregaPrimeiro()
        atual = primeiro
        status(True)
        carregaAbasVeiculo(vei_codigo)
    End Sub

    Private Sub bAnterior_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bAnterior.Click
        Try
            objBLL = New AbastecimentoBLL
            Dim codigo = objBLL.localizarProxAnteriro("anterior", atual)
            objBLL.localizar(codigo)
            If (objBLL.pAba_codigo <> 0) Then
                carregaCampos()
                atual = objBLL.pAba_codigo
                carregaAbasVeiculo(vei_codigo)
            End If
            status(True)
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        Finally
            objBLL = Nothing
        End Try
    End Sub
    Private Sub bProximo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bProximo.Click
        Try
            objBLL = New AbastecimentoBLL
            Dim codigo = objBLL.localizarProxAnteriro("proximo", atual)
            objBLL.localizar(codigo)
            If (objBLL.pAba_codigo <> 0) Then
                carregaCampos()
                atual = objBLL.pAba_codigo
                carregaAbasVeiculo(vei_codigo)
            End If
            status(True)
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        Finally
            objBLL = Nothing
        End Try
    End Sub

    Private Sub bUltimo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bUltimo.Click
        carregaUltimo()
        atual = ultimo
        status(True)
        carregaAbasVeiculo(vei_codigo)
    End Sub

    Private Sub bSair_Click_1(sender As System.Object, e As System.EventArgs) Handles bSair.Click
        Me.Close()
    End Sub

    Private Sub bAjuda_Click(sender As System.Object, e As System.EventArgs) Handles bAjuda.Click
        Try
            Dim Ajuda As String
            Ajuda = Application.StartupPath
            Ajuda = Ajuda.Replace("\bin\Debug", "\Arquivo de Ajuda\index.html#CadastroCargas")
            Dim Ie As Object
            Ie = CreateObject("InternetExplorer.Application")
            Ie.Visible = True
            Ie.Navigate(Ajuda)
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub

    Private Sub bFiltrar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bFiltrar.Click
        Try
            If (dtInicio.Value.Date > dtFinal.Value.Date) Then
                MessageBox.Show("A data inicial não pode ser maior que a final", "Mensagem", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
                dtInicio.Value = Date.Now
                dtFinal.Value = Date.Now
            Else
                objBLL = New AbastecimentoBLL
                Dim tab As DataTable = objBLL.localizar(dtInicio.Value, dtFinal.Value)
                If (tab.Rows.Count > 0) Then
                    dgConsulta.DataSource = tab
                    dgConsulta.Columns("aba_valor").DefaultCellStyle.Format = "R$ ###,###0.00"
                Else
                    MessageBox.Show("Registro(s) não encontrado(s)", "Mensagem", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
                End If
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub

    Private Sub bVeiculos_Click(sender As System.Object, e As System.EventArgs) Handles bVeiculos.Click
        Try
            Dim frm As New frm_consultas
            frm.Text = "Consulta de Veículos"
            frm.tipo = "Veiculos"
            frm.ShowDialog()
            If (frm.codigo <> 0) Then
                objV = New VeiculosBLL
                objV.localizar(frm.codigo)
                vei_codigo = objV.pcodVeiculo
                txtVeiculo.Text = objV.pPlaca
                objV = Nothing
            End If
            frm = Nothing
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub

    Private Sub txtValor_Enter(sender As System.Object, e As System.EventArgs) Handles txtValor.Enter
        Try
            If (txtValor.Text <> "") Then
                Dim valor As Double = txtValor.Text.Replace("R$", "")
                txtValor.Text = valor
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub

    Private Sub txtValor_Leave(sender As System.Object, e As System.EventArgs) Handles txtValor.Leave
        Try
            If (txtValor.Text <> "") Then
                Dim valor As Double = txtValor.Text
                txtValor.Text = valor.ToString("C2")
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub

    Private Sub bClose_Click(sender As System.Object, e As System.EventArgs) Handles bClose.Click
        gbConsulta.Visible = False
        Me.Location = New System.Drawing.Point(Me.Location.X, Me.Location.Y + 127)
        Me.Size = New Size(716, 295)
        bPesquisar.Enabled = True
    End Sub

    Private Sub txtKmAtual_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtKmAtual.KeyPress
        Try
            If Not Char.IsNumber(e.KeyChar) Then
                e.Handled = True
            End If
            If (e.KeyChar = Convert.ToChar(Keys.Enter)) Then
                txtValor.Focus()
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub

    Private Sub txtValor_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtValor.KeyPress
        Try
            If Not Char.IsNumber(e.KeyChar) And e.KeyChar <> Convert.ToChar(Keys.Back) And e.KeyChar <> "," Then
                e.Handled = True
            End If
            If (e.KeyChar = Convert.ToChar(Keys.Enter)) Then
                dtDataAbastecimento.Focus()
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub

    Private Sub dtDataAbastecimento_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles dtDataAbastecimento.KeyPress
        Try
            If (e.KeyChar = Convert.ToChar(Keys.Enter)) Then
                bSalvar.Focus()
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub

    Private Sub bFiltroDescricao_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bFiltroDescricao.Click
        Dim tab As DataTable
        Try
            objBLL = New AbastecimentoBLL
            tab = objBLL.localizar(txtConsultar.Text.Trim())
            If (tab.Rows.Count > 0) Then
                dgConsulta.DataSource = tab
                dgConsulta.Columns("aba_valor").DefaultCellStyle.Format = "R$ ###,###0.00"
            Else
                MessageBox.Show("Registro(s) não encontrado(s)", "Mensagem", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub

    Private Sub dgConsulta_CellDoubleClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgConsulta.CellDoubleClick
        Dim codigo As Integer
        Try
            If (e.RowIndex >= 0) Then
                codigo = dgConsulta.Rows(e.RowIndex).Cells("aba_codigo").Value
                objBLL = New AbastecimentoBLL
                objBLL.localizar(codigo)
                If (objBLL.pAba_codigo <> 0) Then
                    carregaCampos()
                    atual = objBLL.pAba_codigo
                    gbConsulta.Visible = False
                    Me.Location = New System.Drawing.Point(Me.Location.X, Me.Location.Y + 127)
                    Me.Size = New Size(716, 295)
                    bPesquisar.Enabled = True
                End If
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        Finally
            objBLL = Nothing
        End Try
    End Sub
End Class